/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:43
 */

package com.lenovo.mso2o.ui.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.lenovo.framework.base.BaseActionBarMockingActivity;
import com.lenovo.framework.base.InjectableFragment;
import com.lenovo.framework.ui.BridgedX5WebViewFragment;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.conf.JSAction;
import com.lenovo.mso2o.conf.MSConstants;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 */
public class HistoryOrderFragment extends InjectableFragment implements BridgeHandler{

    static boolean historyFilterShow = false;//筛选条件隐藏
    private OrderFilterFragment filterFragment;//历史工单Fragmen
    private BridgedX5WebViewFragment webFragment;//
    private static final String STEP_URL = "http://123.57.223.29/active_job/data.html";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initHistoryDatas();
        return view;
    }

    @Override
    public int provideContentRes() {
        return R.layout.activity_with_fragment;
    }

    @Override
    protected void onFragmentInVisible(Bundle savedInstanceState) {

    }

    @Override
    protected void onFragmentVisible(Bundle savedInstanceState) {

    }

    /**
     * 历史工单控件绑定
     */

    public void initHistoryDatas() {
        filterFragment = OrderFilterFragment.newInstance(getActivity(), MSConstants.FragmentType.FRAGMENT_HISTORYORDERS);
        ((BaseActionBarMockingActivity)getActivity()).setSearchListener(filterFragment.getOrderListFragment());
        //TODO
        webFragment = BridgedX5WebViewFragment.newInstance("about:blank");
        webFragment.setDefaultBridgeHandler(this);
        //replaceFragment(webFragment);
        replaceFragment(filterFragment);
    }

    public void replaceLeftFragment(final Context context) {
        if(webFragment == null) {
            webFragment = BridgedX5WebViewFragment.newInstance(STEP_URL);
            webFragment.setDefaultBridgeHandler(this);
        }
        replaceFragment(webFragment);
        historyFilterShow = false;
    }

    public void replaceRightFragment(final Context context) {
        if(filterFragment == null)
            filterFragment = OrderFilterFragment.newInstance(getActivity(), MSConstants.FragmentType.FRAGMENT_HISTORYORDERS);
        replaceFragment(filterFragment);
        filterFragment.getOrderListFragment().initParams(filterFragment.param, false);
        historyFilterShow = false;
    }

    public void selectHistoryListAndRefresh(){
        replaceRightFragment(getContext());
        filterFragment.onRefresh(true);
    }

    /**
     *refrenced from http://stackoverflow.com/questions/18305945/how-to-resume-fragment-from-backstack-if-exists
     * @param fragment
     */
    private void replaceFragment(Fragment fragment){
        String backStateName = fragment.getClass().getName();

        FragmentManager manager = getChildFragmentManager();
        boolean fragmentPopped = false;
        try {
            fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
        }catch (Exception e){}

        if (!fragmentPopped){ //fragment not in back stack, create it.
            FragmentTransaction ft = manager.beginTransaction();
            ft.replace(R.id.fragment_container, fragment);
            ft.addToBackStack(backStateName);
            ft.commitAllowingStateLoss();
        }
    }

    @Override
    public void handler(String data, CallBackFunction function) {
        JSONObject object = null;
        try {
            object = new JSONObject(data);
            final int code = object.getInt("actionCode");
            if (code == JSAction.REFRESHURL.getValue()) {
                JSONObject datas = object.getJSONObject("data");
                String url = datas.getString("url");
                webFragment.onRefresh(url);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
